package com.card.framework.mybatis.optimist.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.card.framework.mybatis.optimist.LockedByAnotherUserException;
import com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException;


public class MySqlMapper extends Mapper {

        @Override
        public Object getCurrentEntityVersionInDatabase(Connection connection,
                        EntityWrapper entityWrapper) throws SQLException {
                PreparedStatement stmt = null;
                try {
                		stmt = connection
                            .prepareStatement("SELECT "
                                            + entityWrapper.getVersionColumnName() + " FROM "
                                            + entityWrapper.getTableName()
                                            + " WHERE "+entityWrapper.getIdentityColumnName()+" = ? FOR UPDATE");
//                		entityWrapper.getIdentityColumnName()
                        stmt.setObject(1, entityWrapper.getIdentity());
                        stmt.execute();
                        ResultSet rs = stmt.getResultSet();
                        if (rs.first()) {
                                Object databaseVersion = rs.getObject(1);
                                rs.close();
                                return databaseVersion;
                        } else {
                                return null;
                        }
                } catch (MySQLTransactionRollbackException e) {
                        throw new LockedByAnotherUserException();
                } finally {
                        if (stmt != null && !stmt.isClosed()) {
                                stmt.close();
                        }
                }
        }

}